package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.Tesk;
import me.zhengjie.modules.system.domain.TeskAndstudent;
import me.zhengjie.modules.system.domain.Teskandresource;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.mapper.TeskAndstudentMapper;
import me.zhengjie.modules.system.mapper.TeskMapper;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.modules.system.service.TeskAndstudentService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TeskAndstudentServiceImpl extends ServiceImpl<TeskAndstudentMapper, TeskAndstudent> implements TeskAndstudentService {

    private final TeskAndstudentMapper teskAndstudentMapper;

    private final UserMapper userMapper;

    private final TeskMapper teskMapper;
    //获取用户任务列表
    @Override
    public List<Tesk> getStudentTeskList(Integer studentId) {


        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",studentId);
        List<TeskAndstudent> teskAndstudentList = teskAndstudentMapper.selectList(queryWrapper);
        List<Integer> ids = new ArrayList<>();
        for(TeskAndstudent item: teskAndstudentList){
            ids.add(item.getTeskId());
        }
        return teskMapper.selectBatchIds(ids);
    }

    //获取学生任务列表
    @Override
    public List<Tesk> getUserTeskList() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",currentUser.getId());
        List<TeskAndstudent> teskAndstudentList = teskAndstudentMapper.selectList(queryWrapper);
        List<Integer> ids = new ArrayList<>();
        for(TeskAndstudent item: teskAndstudentList){
            ids.add(item.getTeskId());
        }
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("id",ids);
        return teskMapper.selectList(queryWrapper1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void  createTeskAndstudent(TeskAndstudent teskAndstudent){

        save(teskAndstudent);
    }
    @Override
    public void  updateTeskAndstudent(TeskAndstudent resources){
        TeskAndstudent teskAndstudent = getById(resources.getId());
        teskAndstudent.copy(resources);
        Date currentDate = new Date(); // 获取当前时间
        Timestamp currentTimestamp = new Timestamp(currentDate.getTime()); // 转换为Timestamp
        teskAndstudent.setLastUpdateTime(currentTimestamp);
        saveOrUpdate(teskAndstudent);
    }
}
